package com.eclab.exception;

import com.eclab.exception.service.impl.GlobalException;
import com.eclab.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.criterion.NullExpression;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    private Result errorResult = new Result();

    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public Result baseExceptionHandler(HttpServletRequest req,BizException e) {
        errorResult.clean();
        log.info("异常错误的原因是："+e.getErrorMessage());
        errorResult.setCode(e.errorCode);
        errorResult.setMessage(e.errorMessage);
        return errorResult;
    }

    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest request,NullPointerException e) {
        log.info("空指针异常："+e);
        errorResult.clean();
        errorResult.setCode(GlobalException.BODY_NOT_MATCH.getResultCode());
        errorResult.setMessage(GlobalException.BODY_NOT_MATCH.getResultMessage());
        return errorResult;
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest request,Exception e) {
        log.info("未知错误："+e);
        errorResult.clean();
        errorResult.setCode(GlobalException.INTERNAL_SERVER_ERROR.getResultCode());
        errorResult.setMessage(e.getMessage());
        return errorResult;
    }


}
